layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    // 流程定义列表
    var tableIns = table.render({
        elem: '#processDefinitionList',
        url : '/deploy/processDefinitionList',
        cellMinWidth : 65,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 10,
        id : "processDefinitionListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'key', title: '流程定义key', width:120, align:"center"},
            {field: 'name', title: '流程定义名', minWidth:100, align:"center"},
            {field: 'version', title: '版本', width:80, align:"center"},
            {title: '操作', minWidth:175, templet:'#processDefinitionListBar', fixed:"right", align:"center"}
        ]]
    });

    //列表操作
    table.on('tool(processDefinitionList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'start'){ // 启动
            start(data);
        }else if(layEvent === 'showImg'){ // 查看流程图
            showImg(data);
        }else if(layEvent === 'del'){ // 删除
            layer.msg("暂未实现此功能");
        }
    });

    // 启动流程实例
    function start(data){

        // 根据流程定义ID判断显示哪种审核表单
        var content;
        var title;
        if(data.id.indexOf("qjlc") >= 0){
            content = "/page/common/qjlcStart";
            title = "请假单";
        }
        if(data.id.indexOf("sqjflc") >= 0){
            content = "/page/common/sqjflcStart";
            title = "申请经费单";
        }

        var index = layui.layer.open({
            title: title,
            type: 2,
            content: content,
            area: ['520px', '600px'],
            success: function (res) {
                var body = layui.layer.getChildFrame('body', index);
                if (data) {
                    body.find(".key").val(data.key);  // 流程key
                    form.render();
                }
            }
        })
    }

    // 展示流程图
    function showImg(data) {
        //如果流程实例已结束，则提示无法查看流程图
        if(data.endTime != null){
            layer.msg("该流程实例已结束");
            return;
        }
        var index = layui.layer.open({
            title: "查看流程图",
            type: 2,
            content: "/deploy/showImg?processDefinitionId="+data.id,
            area: ['780px', '560px'],
            success: function (res) {

            }
        })
    }
});